import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

// 初始的 state 对象
let initState = {
  // token 的信息对象
  tokenInfo: {},
};
// 获取本地的数据
const stateStr = localStorage.getItem("state");
// 判断是否存在
if (stateStr) {
  initState = JSON.parse(stateStr);
}

export default new Vuex.Store({
  state: {
    tokenInfo: initState,
  },
  mutations: {
    updateTokenInfo: function(state, payload) {
      state.tokenInfo = payload;
      console.log(state);
      this.commit("savetoStorage");
    },
    savetoStorage(state) {
      localStorage.setItem("state", JSON.stringify(state));
    },
  },
  actions: {},
  modules: {},
});
